/*
 * Project: SmartWard
 * Package: com.yesoft.ward.basic.client
 * File: BaseService
 * Copyright © 2018 - 2019 YESOFT CHINA, Inc. All Rights Reserved.
 */
package com.ruoyi.ichen.base.service;

import com.google.common.collect.Lists;
import com.ruoyi.ichen.base.exception.WeServiceException;
import com.ruoyi.ichen.base.utils.MyDateUtils;
import com.ruoyi.ichen.base.utils.MyFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: BaseService
 * @Description: 业务层基类
 * @Author Jun
 * @Date: 2019/4/8 15:15
 * @Version: V1.0
 */
public class BaseService {
//    @Autowired
//    protected RedisService redisService;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected BaseService() {
    }
    /**
     * 获取服务器当前时间
     * @return
     */
    protected Date getCurrentTime() {
        return MyDateUtils.getCurrentDateTime();
    }

    /**
     * 检查上传数组是否为空
     * @param files
     * @return
     */
    protected boolean isMultipartEmpty(MultipartFile[] files) {
        boolean isEmpty = true;
        if (null != files  && files.length > 0) {
            for (MultipartFile file : files) {
                if (file != null && !file.isEmpty()) {
                    isEmpty = false;
                    break;
                }
            }
        }
        return isEmpty;
    }

    /**
     * 检查上传图片
     * @param imgFile
     */
    protected void checkUploadImageFile(MultipartFile imgFile) {
        if (null == imgFile || imgFile.isEmpty()) {
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, "上传文件为空");
        }
        if (StringUtils.isBlank(imgFile.getOriginalFilename())) {
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, "上传文件名为空");
        }
        List<String> fileTypes = Lists.newArrayList();
        fileTypes.add("jpg");
        fileTypes.add("jpeg");
        fileTypes.add("bmp");
        fileTypes.add("png");
        //fileTypes.add(".gif");
        String ext = MyFileUtils.getFileExtension(imgFile.getOriginalFilename());
        if (!fileTypes.contains(ext.toLowerCase())) {
            throw new WeServiceException(HttpStatus.NOT_ACCEPTABLE, "只能上传图片文件");
        }
        /*if (imgFile.getSize() > WebConfig.getUploadSize()) {
            throw new WeServiceException(String.format("文件最大上传为%s", MyFileUtils.formatFileSize(WebConfig.getUploadSize())));
        }*/
    }
}
